package fun.xiefeihong.video.list.service.impl

import fun.xiefeihong.video.list.entity.Role
import fun.xiefeihong.video.list.entity.User
import fun.xiefeihong.video.list.service.UserService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.security.core.userdetails.UserDetails
import org.springframework.security.core.userdetails.UsernameNotFoundException
import org.springframework.security.crypto.password.PasswordEncoder
import org.springframework.stereotype.Service

@Service
class UserServiceImpl implements UserService {

	@Autowired
	PasswordEncoder passwordEncoder

	@Override
	User getByUsername(String username) {
		def roles = []
		def role = new Role('ADMIN', '')
		roles << role
		def password = passwordEncoder.encode('12345678')
		new User('xiefeihong', password, roles, '谢飞鸿')
	}

	@Override
    UserDetails loadUserByUsername(String s) {
		def user = getByUsername(s)
		if(!user){
			throw new UsernameNotFoundException('username not found')
		}
		user
	}

}
